use crate::cljcell::clj_cell::CljCell;
use crate::built_in_functions::my_clojure_interp::{CljFunc, CljRecur, MyClojureInterp, CljFuncParam, CljFuncParamList, MyCljFunc, MyFuncHs, MyCljRecur};
use crate::built_in_functions::util::my_util;
use crate::built_in_functions::my_funs::MyFunctions;
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;

pub struct MyCljData;

impl MyCljData {
    /**
     * first 计算
     * */
    pub fn clj_list_first(interp: &MyClojureInterp, lst: &Vec<Rc<CljCell>>, func_hs: &MyFuncHs, clj_recur: &MyCljRecur, rs: Rc<CljCell>) -> Rc<CljCell> {
        if lst.len() == 2 {
            let c: &Rc<CljCell> = &lst[1];
            match &**c {
                CljCell::List(vs) => {
                    return vs[0].clone();
                },
                CljCell::Symbol(sym_name) => {
                    let rccell: &Rc<CljCell> = func_hs.get(sym_name.as_ref());
                    if let CljCell::List(ls) = &**rccell {
                        if ls.len() > 0 {
                            return ls[0].clone();
                        }
                    }
                },
                _ => return Rc::new(CljCell::Null),
            }
        }
        return Rc::new(CljCell::Null);
    }

    /**
     * rest 计算
     * */
    pub fn clj_list_rest(interp: &MyClojureInterp, lst: &Vec<Rc<CljCell>>, func_hs: &MyFuncHs, clj_recur: &MyCljRecur, rs: Rc<CljCell>) -> Rc<CljCell> {
        if lst.len() == 2 {
            let c: &Rc<CljCell> = &lst[1];

            match &**c {
                CljCell::List(vs) => {
                    let mut vss: Vec<Rc<CljCell>> = Vec::new();
                    for i in 1..vs.len() {
                        vss.push(vs[i].clone());
                    }
                    return Rc::new(CljCell::List(vss));
                },
                CljCell::BracketClj(vs) => {
                    unsafe {
                        println!("{:?}", vs);
                    }
                    let mut vss: Vec<Rc<CljCell>> = Vec::new();
                    for i in 1..vs.len() {
                        vss.push(vs[i].clone());
                    }
                    return Rc::new(CljCell::List(vss));
                },
                CljCell::Symbol(sym_name) => {
                    let rccell: &Rc<CljCell> = func_hs.get(sym_name.as_ref());
                    if let CljCell::List(ls) = &**rccell {
                        let mut vss: Vec<Rc<CljCell>> = Vec::new();
                        for i in 1..ls.len() {
                            vss.push(ls[i].clone());
                        }

                        let r = CljCell::List(vss);
                        return Rc::new(r);
                    }
                },
                _ => return Rc::new(CljCell::Null),
            }
        }
        return Rc::new(CljCell::Null);
    }
}